package com.huawei.spare.part.replace.models;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import java.io.Serializable;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import lombok.Getter;
import lombok.Setter;

public class PerformTask implements Serializable {

  StringProperty index;             // 步骤
  StringProperty name;
  StringProperty step;              // 步骤
  ObjectProperty<Status> status;           // 状态

  @Getter @Setter Observable observable;
  @Getter @Setter Disposable disposable;


  public PerformTask() {
  }

  public PerformTask(Integer index, String name, String step) {
    this.index = new SimpleStringProperty(index.toString());
    this.name = new SimpleStringProperty(name);
    this.step = new SimpleStringProperty(step);
    this.status = new SimpleObjectProperty<Status>(Status.Pending);
  }

  public StringProperty indexProperty() {
    return index;
  }

  public StringProperty stepProperty() {
    return step;
  }

  public StringProperty nameProperty() {
    return name;
  }

  public ObjectProperty<Status> statusProperty() {
    return status;
  }

  public void run(Consumer onNext, Consumer<? super Throwable> onError) {
    dispose();
    if (this.observable != null) {
      this.disposable = this.observable.subscribe(onNext, onError);
    }
  }

  public void dispose() {
    if(this.disposable != null && !this.disposable.isDisposed()) {
      this.disposable.dispose();
    }
  }

  public enum Status {
    Pending("perform.task.status.pending"),
    Waiting("perform.task.status.waiting"),
    Processing("perform.task.status.processing"),
    Completed("perform.task.status.completed"),
    Failed("perform.task.status.failed");

    String display;

    Status(String display) {
      this.display = display;
    }

    public String getDisplay() {
      return display;
    }

  }
}
